const express = require('express')
const app = express()
const async = require('async')
const mysql = require('mysql')
const fs = require('fs')
const path = require('path')
const ejs = require('ejs')
const partials = require('express-partials')

app.use(express.static(path.join(__dirname, 'public')))
app.set('views', path.join(__dirname, 'views'))
app.set('view engine', 'ejs')
app.use(partials())

const pool = mysql.createPool({
    host: '10.19.103.71',
    port: '3306',
    user: 'root',
    password: 'Duo123uclo',
    database: 'hzkb',
    connectionLimit: 10
})

//京东回调接口
app.get('/oauth/callback/jd', function(req, res){
    res.json(JSON.stringify(req.query))
})

app.get('/', function(req, res){
    get_list(req, function(res_data){
        res.render('index.ejs', {products: res_data.data})
    })
})

app.get('/list', function(req, res){
    get_list(req, function(res_data){
        res.send(res_data)
    })
})

let get_list = function(req, process_data){
    let now = new Date().getTime()
    let res_data = ''
    let product_list = []
    let select_sql = ''
    if(!req.query.sinceid){
        select_sql = 'select * from products where pubtime < ' + now + ' order by pubtime desc limit 0,20'
    }else{
        select_sql = 'select * from products where guangdiuid < ' + req.query.sinceid + ' order by pubtime desc limit 0,20'
    }
    pool.getConnection(function(err, connection){
        if(err){

        }else{
            connection.query(select_sql, function(err, results, fields){
                connection.release()
                if(err){

                }else{
                    let i = -1
                    async.whilst(
                        function(){
                            i++
                            return i < results.length
                        },
                        function(whilst_cb){
                            let show_time = get_show_time(results[i].pubtime)
                            let product_info = {
                                id: results[i].id,
                                guangdiuid: results[i].guangdiuid,
                                title: results[i].title,
                                pubtime: show_time,
                                image: results[i].image,
                                fromsite: results[i].fromsite,
                                mall: results[i].mall,
                                description: results[i].description,
                                headinglink: results[i].headinglink,
                                headinglink: results[i].coupontext,
                                couponarea: results[i].couponlink
                            }
                            product_list.push(product_info)
                            whilst_cb(null, 'success')
                        },
                        function(err, result){
                            if(err){

                            }else{
                                res_data = {
                                    code: 0,
                                    msg: '',
                                    data: product_list
                                }
                                process_data(res_data)
                            }
                        }
                    )
                }
            })
        }
    })
}

app.get('/product', function(req, res){
    let res_data = ''
    let select_sql = 'select * from products where id = ' + req.query.id
    pool.getConnection(function(err, connection){
        if(err){

        }else{
            connection.query(select_sql, function(err, results, fields){
                connection.release()
                if(err){
                    
                }else{
                    let show_time = get_show_time(results[0].pubtime)
                    let product_info = {
                        id: results[0].id,
                        guangdiuid: results[0].guangdiuid,
                        title: results[0].title,
                        pubtime: show_time,
                        image: results[0].image,
                        fromsite: results[0].fromsite,
                        mall: results[0].mall,
                        description: results[0].description,
                        headinglink: results[0].headinglink,
                        headinglink: results[0].coupontext,
                        couponarea: results[0].couponlink
                    }
                    res_data = {
                        code: 0,
                        msg: '',
                        data: product_info
                    }
                    res.json(res_data)
                }
            })
        }
    })
})

app.get('/cheap', function(req, res){
    let res_data = ''
    let product_list = []
    let select_sql = ''
    if(!req.query.sincetime){
        select_sql = 'select * from nine_products where pubtime < ' + new Date().getTime() + ' order by pubtime desc limit 0, 20'
    }else{
        select_sql = 'select * from nine_products where pubtime < ' + req.query.sincetime + ' order by pubtime desc limit 0, 20'
    }
    pool.getConnection(function(err, connection){
        if(err){

        }else{
            connection.query(select_sql, function(err, results, fields){
                connection.release()
                let i = -1
                async.whilst(
                    function(){
                        i++
                        return i < results.length
                    },
                    function(whilst_cb){
                        let product_info = {
                            id: results[i].id,
                            title: results[i].title,
                            pubtime: results[i].pubtime,
                            showtime: get_show_time(results[i].pubtime),
                            image: results[i].image,
                            coupontext: results[i].coupontext,
                            price: results[i].price,
                            headinglink: results[i].headinglink
                        }
                        product_list.push(product_info)
                        whilst_cb(null, 'success')
                    },
                    function(err, result){
                        if(err){
                            
                        }else{
                            res_data = {
                                code: 0,
                                msg: '',
                                data: product_list
                            }
                            res.json(res_data)
                        }
                    }
                )
            })
        }
    })
})

function get_show_time(origin_time_data){
    let now = new Date()
    let uptime = new Date(origin_time_data)
    let showtime = ''
    if(now.getDate() == uptime.getDate()){
        showtime = uptime.getHours() + ':' + uptime.getMinutes()
    }else {
        showtime = ('000' + (uptime.getMonth() + 1)).substr(-2) + '-' + ('000' + now.getDate()).substr(-2)
    }
    return showtime
}

const server = app.listen(6666, function(){
    let host = server.address().address
    let port = server.address().port
    console.log('server运行中：http:%s:%s', host, port)
})